package cn.com.guanying.android.controller;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;

import cn.com.guanying.javacore.v11.common.FLog;
/**
 * 
 * @author nieyu
 *
 */
public class HttpProxy 
{
	public static final int TIMEOUT = 30000;//超时
	/**
	 * 这个代理当前打开的url
	 */
	URL mUrl;
	/**
	 * 这个代理当前的连接
	 */
	HttpURLConnection mConnection;
	/**
	 * 当前使用的http method（post ，get）
	 */
	String mMethod;

	public void close() throws IOException
	{
		if(mConnection!=null) {
			mConnection.disconnect();
		}
		mConnection = null;
	}
	
	public String getHeaderField(String name)
	{
		return mConnection.getRequestProperty(name);
	}
	

	public long getLength()
	{
		try
		{
			return mConnection.getInputStream().available();
		}
		catch (IOException e)
		{
			return 0;
		}
	}
	
	public int getResponseCode() throws IOException
	{
		int code = -1;
		try {
			code =mConnection.getResponseCode();
		}catch (SocketTimeoutException e) {
			if(mUrl != null) {
				FLog.e("HttpProxy Timeout: " + mUrl.toString());
			}
			throw e;
		}
		if(code  == -1) {
			FLog.d("HttpProxy.getResponseCode() return -1!");
			if(mUrl != null) {
				FLog.e("HttpProxy Response is -1: " + mUrl.toString());
			}
			throw new IOException("Http Connection has no valid response code!");
		}
		return code;
	}

	public void open(String string) throws IOException, IllegalArgumentException
	{
//		if(NetUtil.isCmwapConnectedOnly()) {
//			openHttpWithProxy(string);
//		}else {
			mUrl = new URL(string);
			mConnection = (HttpURLConnection) mUrl.openConnection();
			mConnection.setReadTimeout( TIMEOUT);
//		}
		
		if (mMethod.equals("POST"))
		{
			mConnection.setDoOutput(true);
		}
		mConnection.setDoInput(true);
		mConnection.setRequestMethod(mMethod);
	}


	public DataInputStream openDataInputStream() throws IOException
	{
		InputStream inputStream;
		inputStream = mConnection.getInputStream();
		return new DataInputStream(inputStream);
	}


	public DataOutputStream openDataOutputStream() throws IOException
	{
		OutputStream outputStream;
		outputStream = mConnection.getOutputStream();
		return new DataOutputStream(outputStream);
	}


	public void setRequestMethod(String method) throws IOException
	{
		mMethod = method;
	}


	public void setRequestProperty(String key, String value) throws IOException
	{
		mConnection.setRequestProperty(key, value);

	}

	/**
	 * 临时将http代理写在平台
	 */
	private void openHttpWithProxy(String url) throws IOException
	{
		int domainStart = 0, domainEnd;
		if (url.startsWith("http://"))
		{
			domainStart = 7;
		}
		domainEnd = url.indexOf('/', domainStart);
		if (domainEnd == -1)
		{
			domainEnd = url.length();
		}
		mUrl = new URL("http://10.0.0.172:80" + url.substring(domainEnd));
		mConnection = (HttpURLConnection)mUrl.openConnection();
		mConnection.setReadTimeout( TIMEOUT);
		mConnection.setRequestProperty("X-Online-Host", url.substring(domainStart, domainEnd));
		mConnection.setRequestProperty("Accept", "*/*");
	}
	
}
